<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <el-tab-pane label="登录账户设置">

            <!-- 放置表单 -->
            <el-form ref="form" v-model="formData" :model="formData" :rules="rules" label-width="120px" style="margin-left: 120px; margin-top:30px">
              <el-form-item label="姓名:" prop="username">
                <el-input v-model="formData.username" style="width:300px" />
              </el-form-item>
              <el-form-item label="密码:" prop="password">
                <el-input v-model="formData.password" style="width:300px" type="password" />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="saveUserInfoClick">更新</el-button>
              </el-form-item>
            </el-form>
          </el-tab-pane>
          <el-tab-pane label="个人详情">
            <user-info />
          </el-tab-pane>
          <el-tab-pane label="岗位信息">
            <job-info />
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
  </div>
</template>

<script>
// 导入获取员工详情数据接口
import { getUserDetailByIdAPI, saveUserDetailByIdAPI } from '@/api/employees'
// 导入个人详情和岗位信息两个表单组件
import UserInfo from './components/user-info.vue'
import JobInfo from './components/job-info.vue'
export default {
  components: {
    UserInfo,
    JobInfo
  },
  data() {
    return {
      // 接收路由传递的员工id
      userId: this.$route.params.id,
      // 员工用户数据
      formData: {
        username: '',
        password: ''
      },
      // 表单验证规则
      rules: {
        username: [{ required: true, message: '姓名不能为空', trigger: 'blur' }],
        password: [{ required: true, message: '密码不能为空', trigger: 'blur' },
          { min: 6, max: 9, message: '密码长度6-9位', trigger: 'blur' }]
      }
    }
  },
  async created() {
    this.getUserDetail()
  },
  methods: {
    // 获取员工详细信息函数
    async  getUserDetail() {
      this.formData = { ...await getUserDetailByIdAPI(this.userId), password: '' }
    },
    // 保存员工数据点击事件函数
    async saveUserInfoClick() {
      // 表单校验
      await this.$refs.form.validate()
      // 发送修改请求
      await saveUserDetailByIdAPI(this.formData)
      // 修改成功提示
      this.$message.success('更新信息成功！')
      //   刷新一下页面
      this.getUserDetail()
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep .el-tabs__item {
  font-size: 16px;
  font-weight: bold;
}
</style>
